package com.atguigu.cloud.order.service.impl;

import com.atguigu.cloud.order.entity.Order;
import com.atguigu.cloud.order.feign.UserFeignClient;
import com.atguigu.cloud.order.mapper.OrderMapper;
import com.atguigu.cloud.order.service.OrderService;
import com.atguigu.cloud.user.entity.User;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author Taeyang
 * @date 2025/4/4
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserFeignClient userFeignClient;

    @Override
    public Order findOrderByOrderId(Long orderId, String header) {

        // 获取订单基本信息
        Order order = baseMapper.selectById(orderId);

        // 传统远程调用（使用IP地址）
        // User user = restTemplate.getForObject("http://localhost:10100/api/user/findUserByUserId/" + order.getUserId(), User.class);

        // 负载均衡远程调用（使用服务名称）
        // User user = restTemplate.getForObject("http://cloud-user/api/user/findUserByUserId/" + order.getUserId(), User.class);

        // OpenFeign远程调用
        User user = userFeignClient.queryById(order.getUserId(), header);

        // 组装订单详细信息
        order.setUser(user);

        return order;
    }
}
